﻿namespace Samples.Application
{
    using System;
    using System.Reflection;
    using System.Web.Hosting;
    using System.Web.Mvc;
    using System.Web.Routing;

    using VirtualTemplates;

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional });
        }

        protected void Application_Start()
        {
#if DEBUG
            HostingEnvironment.RegisterVirtualPathProvider(new VirtualTemplateProvider(
                new MappedVirtualTemplate(AppDomain.CurrentDomain.BaseDirectory + "..\\Samples.Template\\")));
#else
            HostingEnvironment.RegisterVirtualPathProvider(new VirtualTemplateProvider(
            new AssemblyVirtualTemplate(Assembly.Load("Samples.Template"))));
#endif

            HandleVirtualFileModule.FileExtensions.Add(".css");
            HandleVirtualFileModule.FileExtensions.Add(".js");

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}